package com.livegoods.service.impl;

import com.livegoods.commons.pojo.LivegoodsResult;
import com.livegoods.commons.util.ServletUtil;
import com.livegoods.pojo.LoginLog;
import com.livegoods.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.Date;
import java.util.Random;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送验证码思路
     * <p>
     * 1，把当前手机号作为redis的key去查询数据
     * 有：该手机已经发送验证码，给用户提示 ，验证码x分钟有效，请不要频繁发送验证码
     * 没有：生成随4-6机数，把手机号作为key ，随机数作为value存储到redis
     * 2，调用阿里云发送短信服务
     */

    @Override
    public LivegoodsResult sendVerifyCode(String phone) {

        ValueOperations vps = redisTemplate.opsForValue();
        //把当前手机号作为redis的key去查询数据
        String verifyCode = (String) vps.get(phone);
        if (verifyCode != null) {
            return LivegoodsResult.error("验证码3分钟有效，请不要频繁发送验证码");
        }

        //生成随机4位验证码
        StringBuilder sb = new StringBuilder();

        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            sb.append(random.nextInt(10));
        }
        //把手机号作为key ，随机数作为value存储到redis,3分钟过期
        vps.set(phone, sb.toString(), Duration.ofMinutes(3));

        //TODO 使用阿里云发送短信给手机

        return LivegoodsResult.ok(sb.toString());
    }


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 登录逻辑
     * 1，使用手机号作为key去Redis获取对应验证码
     *   没有：提示用户，验证码过期
     *   有：
     *    将用户输入password(验证码)和redis中的随机码必比对
     *
     *    相同：登录成功返回200 逻辑状态码
     *    不相同：提示用户验证码错误
     *
     *
     * @return
     */
    @Override
    public LivegoodsResult login(LoginLog loginLog) {
        loginLog.setType(1);//验证码登录
        //获取请求对象
        HttpServletRequest request = ServletUtil.getRequest();

        String remoteAddr = request.getRemoteAddr();//获取ip
        loginLog.setLoginIp(remoteAddr);

        loginLog.setTime(new Date());



        ValueOperations vps = redisTemplate.opsForValue();
        String verifyCode = (String) vps.get(loginLog.getUsername());
        String msg = null;
        if(verifyCode==null){
            msg = "验证码过期,请重新发送验证码";
            loginLog.setResult(0);
            loginLog.setMsg(msg);
            mongoTemplate.save(loginLog);
            return LivegoodsResult.error(msg);
        }
        //将用户输入password(验证码)和redis中的随机码必比对
        if(!verifyCode.equals(loginLog.getPassword())){
            msg = "手机验证码错误";
            loginLog.setResult(0);
            loginLog.setMsg(msg);
            mongoTemplate.save(loginLog);
            return LivegoodsResult.error(msg);
        }
        loginLog.setMsg("登录成功");
        loginLog.setResult(1);

        mongoTemplate.save(loginLog);

        return LivegoodsResult.ok();
    }


    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            sb.append(random.nextInt(10));
        }
        System.out.println("sb.toString() = " + sb.toString());


    }
}
